package zhoug.zui.gusturelock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import zhoug.logging.Logger;

import androidx.annotation.Nullable;


/**
 * 手势密码中间的圆形View:代表一个密码
 * @Author 35574
 * @Date 2020/10/30
 * @Description
 */
public class ZUIGestureLockView extends View {
    private static final String TAG = ">>>GestureLockView";
    private static final boolean DEBUG = false;

    enum Status {
        NO_FINGER,//没有手指触碰
        FINGER_ON,//手指触碰
        FINGER_UP;//手指抬起
    }

    /*** 当前状态*/
    private Status mCurrentStatus = Status.NO_FINGER;
    private float mDefaultRadius = 100;//默认半径,宽高:wrap_content生效
    /*** 外圆半径:根据组件长宽计算*/
    private float mRadius;
    /*** 内圆半径占比*/
    private float mInnerRadiusRate = 0.3f;
    private int mCenterX, mCenterY;
    /*** 画笔的宽度*/
    private int mStrokeWidth = 3;
    /***点击前内圆颜色*/
    private int mColorNoFingerInner = Color.parseColor("#808080");
    /***点击前外圆颜色*/
    private int mColorNoFingerOuter = Color.parseColor("#A7A7F1");
    /****点中时内圆和外圆颜色*/
    private int mColorFingerOn = Color.parseColor("#2222DD");
    /***手指抬起后内圆和外圆颜色*/
    private int mColorFingerUp = Color.parseColor("#2BD54D");

    private boolean drawInnerCircle=true;//是否绘制内圆
    private Paint mPaint;

    public ZUIGestureLockView(Context context) {
        this(context, null);
    }

    public ZUIGestureLockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZUIGestureLockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        setClickable(true);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画外圆
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        if (mCurrentStatus == Status.FINGER_ON) {
            mPaint.setColor(mColorFingerOn);
        } else if (mCurrentStatus == Status.FINGER_UP) {
            mPaint.setColor(mColorFingerUp);
        } else {
            mPaint.setColor(mColorNoFingerOuter);
        }
        canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);

        //画内圆
        if(drawInnerCircle){
            mPaint.setStyle(Paint.Style.FILL);
            if (mCurrentStatus == Status.FINGER_ON) {
                mPaint.setColor(mColorFingerOn);
            } else if (mCurrentStatus == Status.FINGER_UP) {
                mPaint.setColor(mColorFingerUp);
            } else {
                mPaint.setColor(mColorNoFingerInner);
            }
            canvas.drawCircle(mCenterX, mCenterY, mRadius * mInnerRadiusRate, mPaint);
        }


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = getMeasuredWidth() >> 1;
        mCenterY = getMeasuredHeight() >> 1;
        int contentW = getMeasuredWidth() - getPaddingStart() - getPaddingEnd();
        int contentH = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        mRadius = Math.min(contentH, contentW) - mStrokeWidth >> 1;

        if (DEBUG) {
            Logger.d(TAG, "onSizeChanged:w=" + w + ",h=" + h + "=>mRadius=" + mRadius);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            //宽高确定
            int contentW = widthSize - getPaddingStart() - getPaddingEnd();
            int contentH = heightSize - getPaddingTop() - getPaddingBottom();
            mRadius = Math.min(contentH, contentW) - mStrokeWidth >> 1;
        } else if (widthMode == MeasureSpec.EXACTLY) {
            //宽度确定
            int height = widthSize;
            if (heightSize > 0) {
                height = Math.min(widthSize, heightSize);
            }
            setMeasuredDimension(widthSize, height);
        } else if (heightMode == MeasureSpec.EXACTLY) {
            //高度确定
            int width = heightSize;
            if (widthSize > 0) {
                width = Math.min(widthSize, heightSize);
            }
            setMeasuredDimension(width, heightSize);
        } else {
            //宽高不确定 wrap_content
            int width = (int) (mDefaultRadius + getPaddingStart() + getPaddingEnd());
            if (widthSize > 0) {
                width = Math.min(widthSize, width);
            }
            int height = (int) (mDefaultRadius + getPaddingTop() + getPaddingBottom());
            if (heightSize > 0) {
                height = Math.min(heightSize, height);
            }
            setMeasuredDimension(width, height);
        }
    }

   /* @Override
    public void setPressed(boolean pressed) {
        if (pressed) {
            mCurrentStatus = Status.FINGER_ON;
            removeCallbacks(runnable);
        } else {
            mCurrentStatus = Status.FINGER_UP;
            removeCallbacks(runnable);
            postDelayed(runnable, 500);
        }
        invalidate();
        super.setPressed(pressed);
    }*/

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mCurrentStatus = Status.NO_FINGER;
            invalidate();
        }
    };

    /**
     * 设置状态
     *
     * @param status {@link Status}
     */
    public void setStatus(Status status) {
        this.mCurrentStatus = status;
        invalidate();
    }

    /**
     * 设置内圆半径占比
     *
     * @param innerRadiusRate
     */
    public void setInnerRadiusRate(float innerRadiusRate) {
        this.mInnerRadiusRate = innerRadiusRate;
    }

    /**
     * 画笔粗细
     * @param strokeWidth
     */
    public void setStrokeWidth(int strokeWidth) {
        this.mStrokeWidth = strokeWidth;
    }

    /**
     * 点击前内圆颜色
     * @param colorNoFingerInner
     */
    public void setColorNoFingerInner(int colorNoFingerInner) {
        this.mColorNoFingerInner = colorNoFingerInner;
    }

    /**
     * 点击前外圆颜色
     * @param colorNoFingerOuter
     */
    public void setColorNoFingerOuter(int colorNoFingerOuter) {
        this.mColorNoFingerOuter = colorNoFingerOuter;
    }


    /**
     * 点中时内圆和外圆颜色
     * @param colorFingerOn
     */
    public void setColorFingerOn(int colorFingerOn) {
        this.mColorFingerOn = colorFingerOn;
    }

    /**
     * 手指抬起后内圆和外圆颜色
     * @param colorFingerUp
     */
    public void setColorFingerUp(int colorFingerUp) {
        this.mColorFingerUp = colorFingerUp;
    }

    public void setDrawInnerCircle(boolean drawInnerCircle) {
        this.drawInnerCircle = drawInnerCircle;
    }
}
